<!-- 多个柱状图 https://echarts.apache.org/examples/zh/editor.html?c=dataset-simple1 -->
<!-- https://echarts.apache.org/examples/zh/editor.html?c=dataset-simple0-->
<template>
  <div class="chart-container">
    <div ref="chart" class="chart"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";

export default {
  props: {
    list: {
      type: Array,
      required: true,
    },
  },
  mounted() {
    this.renderChart();
  },
  watch: {
    list: {
      handler() {
        this.renderChart();
      },
      deep: true,
    },
  },
  methods: {
    renderChart() {
      const chartDom = this.$refs.chart;
      const myChart = echarts.init(chartDom, "");
      const option = this.generateOption();
      myChart.setOption(option);
      window.addEventListener("resize", () => {
        myChart.resize();
      });
    },
    generateOption() {
      const categories = this.list.map((item) => item.platformName);
      const basePriceData = this.list.map((item) => item.basePrice);
      const peakHourSurchargeData = this.list.map((item) => item.peakHourSurcharge);
      const nighttimeSurchargeData = this.list.map((item) => item.nighttimeSurcharge);

      const option = {
        title: {
          text: "价格对比",
        },
        legend: {},
        tooltip: {},
        dataset: {
          source: [
            ['平台名', ...categories],
            ['起步价', ...basePriceData],
            ['高峰时段加价', ...peakHourSurchargeData],
            ['夜间加价', ...nighttimeSurchargeData]
          ],
        },
        xAxis: { type: "category" },
        yAxis: {},
        series: [{ type: "bar" }, { type: "bar" }, { type: "bar" }, { type: "bar" }],
      };

      return option;
    },
  },
};
</script>
<style lang="scss" scoped>

</style>
